package chapter13.section8;
/*程序13-7 使用JdbcRowSet访问数据库*/

import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
import java.sql.SQLException;

public class JdbcRowSetDemo {

    public static void main(String[] args) throws SQLException {
        RowSetFactory aFactory = RowSetProvider.newFactory();
        JdbcRowSet jdbcRs = aFactory.createJdbcRowSet();
        jdbcRs.setUrl("jdbc:mysql://localhost:3306/bank");
        jdbcRs.setUsername("root");
        jdbcRs.setPassword("123456");
        jdbcRs.setCommand("select * from tb_account");
        jdbcRs.execute();
        while (jdbcRs.next()) {
            System.out.println(jdbcRs.getString("name"));
        }
        jdbcRs.setAutoCommit(false);
        jdbcRs.moveToInsertRow();
        jdbcRs.updateString("id", "11");  //注意关键字的值不要和已有记录重复
        jdbcRs.updateString("name", "鲁宁");
        jdbcRs.updateInt("balance", 100);
        jdbcRs.insertRow();
        jdbcRs.commit();
        jdbcRs.close();
    }
}